<?php

use think\Cache;
use think\Session;
use app\admin\api\AppToken;

    //根据站点语言来设置添加和更新信息的模板
    function getlang($sid){
        //获取站点语言
        $lan = Session::get("lang[$sid]");

        $lang['lang'] = $lan;
        if($lang['lang'] == 'en,zh-CN'){
            $lang['hy']['0']['lan'] = 'en';
            $lang['hy']['0']['title'] = 'title';
            $lang['hy']['0']['content'] = 'content';
            $lang['hy']['0']['currency'] = 'Currency type';
            $lang['hy']['0']['coatname'] = 'Cost name';
            $lang['hy']['0']['coatprice'] = 'Cost price';
            $lang['hy']['0']['accompany_price'] = 'accompany_price';
            $lang['hy']['1']['lan'] = 'zh-CN';
            $lang['hy']['1']['title'] = '标题';
            $lang['hy']['1']['content'] = '内容';
            $lang['hy']['1']['currency'] = '货币类型';
            $lang['hy']['1']['coatname'] = '费用名称';
            $lang['hy']['1']['coatprice'] = '标准费用';
            $lang['hy']['1']['accompany_price'] = '陪同费用';
        }else if($lang['lang'] == 'zh-CN'){
            $lang['hy']['0']['lan'] = 'zh-CN';
            $lang['hy']['0']['title'] = '标题';
            $lang['hy']['0']['content'] = '内容';
            $lang['hy']['0']['currency'] = '货币类型';
            $lang['hy']['0']['coatname'] = '费用名称';
            $lang['hy']['0']['coatprice'] = '标准费用';
            $lang['hy']['0']['accompany_price'] = '陪同费用';
        }else{
            $lang['hy']['0']['lan'] = 'en';
            $lang['hy']['0']['title'] = 'title';
            $lang['hy']['0']['content'] = 'content';
            $lang['hy']['0']['currency'] = 'Currency type';
            $lang['hy']['0']['coatname'] = 'Cost name';
            $lang['hy']['0']['coatprice'] = 'Cost price';
            $lang['hy']['0']['accompany_price'] = 'accompany_price';
        }

        return $lang;
    }

    //将获取到的内容组合成前台可用的样式（论文）
    function getstandardpaper($site){
        foreach ($site as $key => $value){
            $intrs[$key]['status'] = $value->status;
            $intrs[$key]['status_name'] = $value->status_name;
            $intrs[$key]['uid'] = $value->uid;
            $intrs[$key]['number'] = $value->number;

            foreach($value->authors as $k => $v){
                $intrs[$key]['authors'][$key]['email'] = $value->authors[$k]->email;
                $intrs[$key]['authors'][$key]['title'] = $value->authors[$k]->title;
                $intrs[$key]['authors'][$key]['first_name'] = $value->authors[$k]->first_name;
                $intrs[$key]['authors'][$key]['middle_name'] = $value->authors[$k]->middle_name;
                $intrs[$key]['authors'][$key]['last_name'] = $value->authors[$k]->last_name;
                $intrs[$key]['authors'][$key]['country'] = $value->authors[$k]->country;
            }

            if(isset($value->abstract)){
                $intrs[$key]['abstract']['uid'] = $value->abstract->uid;
                $intrs[$key]['abstract']['file_url'] = $value->abstract->file_url;
                $intrs[$key]['abstract']['status'] = $value->abstract->status;
                $intrs[$key]['abstract']['status_name'] = $value->abstract->status_name;
                $intrs[$key]['abstract']['created_at'] = $value->abstract->created_at;
            }

            if(isset($value->translations[1])){

                if($value->translations[0]->locale == 'en'){

                    $intrs[$key]['translations']['zhlang'] = $value->translations[1]->locale;
                    $intrs[$key]['translations']['zhtitle'] = $value->translations[1]->title;
                    $intrs[$key]['translations']['zhdesc'] = $value->translations[1]->remark;

                    $intrs[$key]['translations']['enlang'] = $value->translations[0]->locale;
                    $intrs[$key]['translations']['entitle'] = $value->translations[0]->title;
                    $intrs[$key]['translations']['endesc'] = $value->translations[0]->remark;
                }else{
                    $intrs[$key]['translations']['zhlang'] = $value->translations[0]->locale;
                    $intrs[$key]['translations']['zhtitle'] = $value->translations[0]->title;
                    $intrs[$key]['translations']['zhdesc'] = $value->translations[0]->remark;

                    $intrs[$key]['translations']['enlang'] = $value->translations[1]->locale;
                    $intrs[$key]['translations']['entitle'] = $value->translations[1]->title;
                    $intrs[$key]['translations']['endesc'] = $value->translations[1]->remark;
                }

            }else if(isset($value->translations[0])){

                if($value->translations[0]->locale == 'en'){
                    $intrs[$key]['translations']['enlang'] = $value->translations[0]->locale;
                    $intrs[$key]['translations']['entitle'] = $value->translations[0]->title;
                    $intrs[$key]['translations']['endesc'] = $value->translations[0]->remark;
                    $intrs[$key]['translations']['zhlang'] = '--';
                    $intrs[$key]['translations']['zhtitle'] = '--';
                    $intrs[$key]['translations']['zhdesc'] = '--';
                }else{
                    $intrs[$key]['translations']['zhlang'] = $value->translations[0]->locale;
                    $intrs[$key]['translations']['zhtitle'] = $value->translations[0]->title;
                    $intrs[$key]['translations']['zhdesc'] = $value->translations[0]->remark;

                    $intrs[$key]['translations']['enlang'] = '--';
                    $intrs[$key]['translations']['entitle'] = '--';
                    $intrs[$key]['translations']['endesc'] = '--';
                }
            }else{
                $intrs[$key]['translations']['enlang'] = '--';
                $intrs[$key]['translations']['entitle'] = '--';
                $intrs[$key]['translations']['endesc'] = '--';
                $intrs[$key]['translations']['zhlang'] = '--';
                $intrs[$key]['translations']['zhtitle'] = '--';
                $intrs[$key]['translations']['zhdesc'] = '--';
            }

            $intrs[$key]['created'] = date("Y-m-d H:i",$value->created_at);
        }
        return $intrs;
    }


//将获取到的内容组合成前台可用的样式（论文摘要）
function getstandardpaperabstract($site){

    foreach ($site as $key => $value){
        $intrs[$key]['number'] = $key;
        $intrs[$key]['status'] = $value->status;
        $intrs[$key]['status_name'] = $value->status_name;
        $intrs[$key]['uid'] = $value->uid;
        $intrs[$key]['file_url'] = $value->file_url;
        $intrs[$key]['created'] = date("Y-m-d H:i",$value->created_at);

    }
    return $intrs;
}


    //将获取到的内容组合成前台可用的样式（会议管理）
    function getstandardconent($site){
        foreach ($site as $key => $value){
            $intrs[$key]['status'] = $value->status;//1为正常
            $intrs[$key]['uid'] = $value->uid;
            $intrs[$key]['number'] = $key;
            if(!empty($value->code)){
                $intrs[$key]['code'] = $value->code;
            }
            if(!empty($value->module)){
                $intrs[$key]['module'] = $value->module;
            }
            if(!empty($value->position)){
                $intrs[$key]['position'] = $value->position;
            }
            if(!empty($value->target_id)){
                $intrs[$key]['target_id'] = $value->target_id;
            }
            if(!empty($value->parent_id)){
                $intrs[$key]['parent_id'] = $value->parent_id;
            }
            if(isset($value->translations[1])){
                if($value->translations[1]->locale == 'zh-CN') {
                    $intrs[$key]['zhlang'] = $value->translations[1]->locale;
                    $intrs[$key]['zhtitle'] = empty($value->translations[1]->title) ? $value->translations[1]->name : $value->translations[1]->title;
                    $intrs[$key]['zhdesc'] = isset($value->translations[1]->content) ? $value->translations[1]->content : $value->translations[1]->description;

                    $intrs[$key]['enlang'] = $value->translations[0]->locale;
                    $intrs[$key]['entitle'] = empty($value->translations[0]->title) ? $value->translations[0]->name : $value->translations[0]->title;
                    $intrs[$key]['endesc'] = isset($value->translations[0]->content) ? $value->translations[0]->content : $value->translations[0]->description;
                }else{
                    $intrs[$key]['zhlang'] = $value->translations[0]->locale;
                    $intrs[$key]['zhtitle'] = empty($value->translations[0]->title) ? $value->translations[0]->name : $value->translations[0]->title;
                    $intrs[$key]['zhdesc'] = isset($value->translations[0]->content) ? $value->translations[0]->content : $value->translations[0]->description;

                    $intrs[$key]['enlang'] = $value->translations[1]->locale;
                    $intrs[$key]['entitle'] = empty($value->translations[1]->title) ? $value->translations[1]->name : $value->translations[1]->title;
                    $intrs[$key]['endesc'] = isset($value->translations[1]->content) ? $value->translations[1]->content : $value->translations[1]->description;
                }

            }else if(isset($value->translations[0])){

                if($value->translations[0]->locale == 'en'){
                    $intrs[$key]['enlang'] = $value->translations[0]->locale;
                    $intrs[$key]['entitle'] = empty($value->translations[0]->title)?$value->translations[0]->name:$value->translations[0]->title;
                    $intrs[$key]['endesc'] = isset($value->translations[0]->content)?$value->translations[0]->content:$value->translations[0]->description;
                    $intrs[$key]['zhlang'] = '--';
                    $intrs[$key]['zhtitle'] = '--';
                    $intrs[$key]['zhdesc'] = '--';
                }else{
                    $intrs[$key]['zhlang'] = $value->translations[0]->locale;
                    $intrs[$key]['zhtitle'] = empty($value->translations[0]->title)?$value->translations[0]->name:$value->translations[0]->title;
                    $intrs[$key]['zhdesc'] = isset($value->translations[0]->content)?$value->translations[0]->content:$value->translations[0]->description;

                    $intrs[$key]['enlang'] = '--';
                    $intrs[$key]['entitle'] = '--';
                    $intrs[$key]['endesc'] = '--';
                }
            }else{
                $intrs[$key]['enlang'] = '--';
                $intrs[$key]['entitle'] = '--';
                $intrs[$key]['endesc'] = '--';
                $intrs[$key]['zhlang'] = '--';
                $intrs[$key]['zhtitle'] = '--';
                $intrs[$key]['zhdesc'] = '--';
            }

            $intrs[$key]['created'] = date("Y-m-d H:i",$value->created_at);
        }
        return $intrs;
    }


    //设置header头文件（用于guzzlehttp传输用）
    function getheader($token){
        //登录认证
        $access_token = Session::get('user')->token->access_token;
        $headers['Authorization'] = $access_token;

        //站点管理token认证
        $headers['AuthToken'] = $token->auth_token;
        $headers['AuthorizeToken'] = $token->authorize_token;

        return $headers;
    }


    //换取appde的token
    function getapptoken($sid){

        $token = Cache::get("appToken[$sid]");

        if(isset($token)){

            $app_id = Session::get("appid[$sid]");

            $to = new \app\admin\model\token();
            $app_secret = $to->where("app_id=$app_id")->value('app_secret');

            $dat['app_id'] = $app_id;
            $dat['app_secret'] = $app_secret;

            //换区网站token
            $data = new AppToken();
            $token = $data->index($dat);

           Cache::set("appToken[$sid]",$token,3600);
        }

        return $token;
    }

